<?php

/*
 * Action Attack
 * 
 * 3$
 * 
 * Each player (including you) reveals the top 2 cards of his deck, and you choose one: either he discards them, or he puts them back on top in an order he chooses.
 * +2 Cards
 * 
 */

class Oracle extends Card implements iAction, iKingdom, iAttack, iHinterlands {

    public function __construct() {
        parent::__construct("Oracle", 3);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
            "Each player (including you) reveals the top 2 cards of his deck, and you choose one: either he discards them, or he puts them back on top in an order he chooses.",
            "+2 Cards");
    }

    public function play(Game $game, $isRealCard) {
        $game->attack($this);

        $names = $game->getPlayersNames(true);
        $return_cards_players = array();
        foreach ($names as $name) {
            $cards = $game->removeCardsFromDrawDeck(2, $name);

            if (sizeof($cards) > 0) {

                ScreenDisplay::addScreenMessage($game, true, "reveal " . nicePrint($cards), "reveals " . ScreenDisplay::$LMSG, $name);
                $options = array(new Option(1, "Yes"), new Option(2, "No"));

                $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, "Discard " . niceReadableString($game, $cards) . " to " . $name . "?");
                if ($output[0] == 'Yes') {
                    $game->discardTempDiscardCards($cards, $name);
                    ScreenDisplay::addScreenMessage($game, true, " discard " . nicePrint($cards), "discards " . ScreenDisplay::$LMSG, $name);
                } else {
                    $return_cards_players[$name] = $cards;
                }
            }
        }

        foreach ($return_cards_players as $name => $cards) {
            $output = MySocket::GetUserReply($game, Input_Reorder, $cards, -1, -1, null, "Put cards back in any order", $name, true);
        }

        $users_replies = MySocket::getUsersReplies($game);
        foreach ($users_replies as $name => $output) {

            $output = array_reverse($output);
            foreach ($output as $p) {
                $game->putCardOnTopOfDrawDeck($p, $name);
            }
            ScreenDisplay::addScreenMessage($game, true, "put " . nicePrint($output) . " on top of your deck", "puts " . sizeof($output) . " cards on top of his deck", $name);
        }

        $drawed_cards = $game->drawCards(2);
        ScreenDisplay::addScreenMessage($game, true, "draw " . nicePrint($drawed_cards), "draws " . sizeof($drawed_cards) . " cards");

        $game->cardActionFinished();
        return true;
    }

}